#include <iostream>
#include <string.h>
#include <queue>
using namespace std;


int cac(int a,int b){
	if(a%b == 0)
		return b;
	else
		return cac(b,a%b);
}



struct tp{
	int p,q,r,b;
	int lweight=-1,rweight=-1;
};


int main(){
	int weight=0;
	int n;
	cin>>n;
	tp a[n];
	int f[n];
	char lf[n];
	queue<int> q;
	memset(f,-1,sizeof(f));
	for(int i=0;i<n;i++){
		cin>>a[i].p>>a[i].q>>a[i].r>>a[i].b;
		a[i].r--;
		a[i].b--;
		int l;
		l = cac(a[i].p,a[i].q);
		a[i].p /= l;
		a[i].q /= l;
		if(a[i].r == -1  &&  a[i].b == -1){
			a[i].lweight = 1;
			a[i].rweight = 1;
			q.push(i);
			continue;
		}
		if(a[i].r == -1){
			a[i].lweight = 1;
			f[a[i].b] = i;
			lf[a[i].b] = 'r';
			continue;
		}
		if(a[i].b == -1){
			a[i].rweight = 1;
			f[a[i].r] = i;
			lf[a[i].r] = 'l';
			continue;
		}
		f[a[i].b] = i;
		lf[a[i].b] = 'r';
		f[a[i].r] = i;
		lf[a[i].r] = 'l';
	}
	
	while(!q.empty()){
		int now;
		now = q.front();
		q.pop();
//		cout<<now<<endl;
		int l,r;
		l = a[now].lweight;
		r = a[now].rweight;
		l *= a[now].p;
		r *= a[now].q;
		int c;
		c = cac(l,r);
		l /= c;
		r /= c;
		if(f[now] != -1){
			if(lf[now] == 'l')
				a[f[now]].lweight = l*a[now].rweight+r*a[now].lweight;
			else
				a[f[now]].rweight = l*a[now].rweight+r*a[now].lweight;
		}
		if(f[now] == -1){
			weight = l*a[now].rweight+r*a[now].lweight;
			break;
		}
		if(a[f[now]].lweight != -1  &&  a[f[now]].rweight != -1)
			q.push(f[now]);
	}
	cout<<weight;
	return 0;
}
